<template>
	<view class="product-detail">
		<view>
			<u-toast ref="uToast" />
		</view>
		<!-- 模态框提示完善收货地址 -->
		<view>
			<u-modal v-model="showModal" :content="content" :mask-close-able="true" z-index="999999"
				:show-cancel-button="true" confirm-text="去完善" confirm-color="#FC5E14" @confirm="finishInfo"></u-modal>
		</view>
		<view v-if="showPage">
			<view class="pro-info">
				<view class="pro-img">
					<image :src="proInfo.img_url" mode="widthFix"></image>
				</view>
				<view class="info">
					<!-- 包邮和打折 -->
					<view class="free-discount">
						<text class="discount" v-if="isSell">打折</text>
						<text class="free">包邮</text>
					</view>
					<view class="title">
						{{proInfo.title}}
					</view>
					<view class="count" v-if="proInfo.productFeatures">
						{{proInfo.productFeatures}}
					</view>
				</view>
				<view class="price-box">
					<view class="price">
						<text class="small">￥</text>
						<text class="big">{{dealPrice(proInfo.sellPrice)[0]}}.</text>
						<text class="small">{{dealPrice(proInfo.sellPrice)[1]}}</text>
					</view>
					<view class="oldprice" v-if="isSell">
						<text>原价：</text>
						<text>￥{{(proInfo.price / 100).toFixed(2) }}</text>
					</view>
				</view>
			</view>
			<view class="pro-desc" v-html="proInfo.pro_desc">
			</view>
			<view class="paybar">
				<view class="customer" @click="show=true">
					<view class="iconfont icon-lianxikefu">
					</view>
					<view>
						联系客服
					</view>
				</view>
				<view class="paybtn" @click="showPay=true">
					立即购买
				</view>
			</view>

		</view>
		<view class="empty" v-else>
			<u-empty text="库存位空或者商品已下架" mode="data"></u-empty>
		</view>
		<!-- 弹出层 -->
		<u-popup v-model="show" mode="bottom" border-radius="14">
			<view class="show" @click="toggleShow">
				<view class="kefupic">
					<view class="pic-title">
						商城客服
					</view>
					<view class="pic-info">
						<image src="../../static/image/kefu_sc.jpg" mode="widthFix"></image>
					</view>
				</view>
				<view class="kefupic">
					<view class="pic-title">
						qq客服
					</view>
					<view class="pic-info">
						<image src="../../static/image/kefu_qq.jpg" mode="widthFix"></image>
					</view>
				</view>
				<view class="kefupic">
					<view class="pic-title">
						微信客服
					</view>
					<view class="pic-info">
						<image src="../../static/image/wx_kf.jpg" mode="widthFix"></image>
					</view>
				</view>
			</view>
		</u-popup>
		<u-popup v-model="showPay" mode="bottom" border-radius="14">
			<view class="showpay">
				<view class="pro-info">
					<view class="pro-pic">
						<image :src="proInfo.img_url" mode="widthFix"></image>
					</view>
					<view class="pro-price">
						<view class="price">
							<text class="big">￥{{dealPrice(proInfo.sellPrice)[0]}}.</text>
							<text class="small">{{dealPrice(proInfo.sellPrice)[1]}}元</text>
						</view>
						<view class="specifications">
							规格：{{format}}
						</view>
					</view>
				</view>
				<view class="cations">
					<view class="cationstitle">
						规格分类
					</view>
					<view class="cationsdesc">
						{{format}}
					</view>
				</view>
				<view class="paycount">
					<view class="title">
						购买数量
					</view>
					<view class="counter">
						<u-number-box v-model="count" :min="1" bg-color="#FC5E14" color="#fff"></u-number-box>
					</view>
				</view>
				<view class="remarks">
					<view class="title">
						备注
					</view>
					<view class="ipt">
						<u-input v-model="remark" type="text" :clearable="false" input-align="right"
							placeholder="请输入备注信息" :custom-style="{fontSize: '24rpx'}" />
					</view>
				</view>
				<view class="useroption">
					<view class="addcart" @click="addShopCart">
						加入购物车
					</view>
					<view class="pay" @click="pay">
						立即支付
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		getLocalData,
		setLocalData
	} from "@/util/localStroage.js"
	import request from '@/util/request.js'
	export default {
		data() {
			return {
				uuid: '',
				loginStatus: true,
				showModal: false, //是否展示模态框
				content: '', // 模态框展示的信息
				proInfo: {},
				formatId: '', // 商品的
				format: '',
				show: false, // 展示客服面板
				showPay: false, // 展示支付面板
				showPage: false,
				count: 1, // 商品数量
				remark: '' // 备注信息
			}
		},
		computed: {
			isSell() {
				if (this.proInfo.price == this.proInfo.sellPrice) {
					return false
				}
				return true
			}
		},
		methods: {
			showRes(title, type, callback) {
				this.$refs.uToast.show({
					title,
					type: type ? type : "default",
					duration: 2000,
					position: "top",
					callback: callback ? callback : () => {}
				})
			},
			showTip(title, type, callback) {
				this.$refs.uToast.show({
					title,
					type,
					duration: 1000,
					position: "top",
					callback: callback ? callback : () => {}
				})
			},
			requestProDetail() {
				request.globalRequest({
					url: "/api/public/mall/product/get",
					data: {
						uuid: this.uuid
					}
				}).then(res => {
					if (res.status == "error.2205") {
						return this.showTip(res.message, "default")
					}
					this.showPage = true
					const product = res.product
					let proInfo = product.productInfo
					proInfo = proInfo.replace(/src="/ig, 'src="http://yihao.cdn.gd-zjgj.com')
					this.proInfo = {
						img_url: "http://yihao.cdn.gd-zjgj.com" + product.productImg,
						pro_desc: proInfo,
						price: product.productPrice,
						sellPrice: product.productSellPrice,
						title: product.productTitle,
						name: product.mallMerchantName,
						productFeatures: product.productFeatures

					}
					this.format = res.format[0].formatName
					this.formatId = res.format[0].formatId
				})
			},
			dealPrice(price) {
				return (price / 100).toFixed(2).split('.')
			},
			toggleShow() {
				this.show = false
			},
			// 获取用户信息
			getUserInfo() {
				request.globalRequest({
					url: "/api/member/user/info"
				}).then(res => {
					console.log(res);
					if (res.status == "login") {
						this.loginStatus = false
					}

				})
			},
			addShopCart() {
				if (!this.loginStatus) {
					return uni.navigateTo({
						url: "/pages/login/login"
					})
				}
				if (this.count < 1 || typeof this.count !== "number") {
					return this.showRes("商品数量错误", "error")
				}
				request.globalRequest({
					url: "/api/mall/product/order_add",
					method: "post",
					data: {
						uuid: this.uuid,
						formatId: this.formatId,
						buyNum: this.count,
						info: this.remark,
						insured: 1
					}
				}).then(res => {
					console.log(res, this.uuid);

					if (!res.status) {
						this.showRes("加入购物车成功", "success", () => {
							this.count = 1
							this.remark = ''
							uni.switchTab({
								url: "/pages/shopcart/shopcart"
							})
						})
						// 加入购物车成功
					} else {
						this.showModal = true
						this.content = res.message
					}
				})
			},
			// 去完善地址信息
			finishInfo() {
				console.log("完善");
				uni.navigateTo({
					url: '/pages/perfect/perfect'
				})
			},
			pay() {
				if (!this.loginStatus) {
					return uni.navigateTo({
						url: "/pages/login/login"
					})
				}
				this.reuquestPay()
			},
			setId() {
				const date = new Date()
				return "" + date.getFullYear() + (date.getMonth() + 1) + date.getDate() + date.getTime()
			},
			// 请求支付
			reuquestPay() {
				let data = getLocalData("orderList")
				const id = this.setId()
				const order = {
					id: id,
					status: 1,
					proList: [{
						title: this.proInfo.title,
						image: this.proInfo.img_url,
						count: this.count,
						uuid: this.uuid,
						remark: this.remark,
						format: this.format,
						sellPrice: this.proInfo.sellPrice
					}]
				}
				data.unshift(order)
				this.remark = ''
				setLocalData("orderList", data)
				uni.navigateTo({
					url: "/pages/orderDetail/orderDetail?id=" + id
				})
			}
		},

		onLoad() {
			const routes = getCurrentPages()
			this.uuid = routes[0].$route.query.uuid
			this.requestProDetail()
			this.getUserInfo()
		}
	}
</script>

<style lang="scss" scoped>
	.product-detail {
		padding: 0 20rpx 100rpx;

		.empty {
			padding-top: 300rpx;
		}

		.showpay,
		.show {
			padding: 20rpx;

			.kefupic {
				margin-bottom: 20rpx;
				color: #777;
				font-size: 32rpx;
				text-align: center;

				.pic-title {
					line-height: 48rpx;
				}


				.pic-info {
					width: 300rpx;
					margin: 0 auto;

					image {
						width: 100%;
					}
				}

			}

			.pro-info {
				display: flex;

				.pro-pic {
					width: 160rpx;
					border: 1px solid #E7E7E7;
					border-radius: 20rpx;

					image {
						width: 100%;
					}
				}

				.pro-price {
					flex: 1;
					margin-left: 20rpx;

					.price {
						color: #FC5E14;
						line-height: 48rpx;

						.big {
							font-size: 36rpx;
							font-weight: 700;
						}

						.small {
							font-size: 24rpx;
						}
					}

					.specifications {
						color: #000;
						font-size: 24rpx;
					}
				}
			}

			.cations {
				margin-top: 20rpx;
				border-top: 1px solid #DEDEDE;
				padding: 10rpx 0;

				.cationstitle {
					font-size: 28rpx;
					color: #000;
					font-weight: 600;
				}

				.cationsdesc {
					margin-top: 20rpx;
					border-radius: 10rpx;
					border: 1px solid #FC5E14;
					padding: 10rpx 20rpx;
					font-size: 28rpx;
					font-weight: 700;
					color: #FC5E14;
				}
			}

			.paycount,
			.remarks {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 10rpx 0;
				margin-top: 10rpx;
				border-top: 1px solid #DEDEDE;

				.title {
					font-size: 28rpx;
					font-weight: 700;

				}

				.ipt {
					margin-left: 20rpx;
					flex: 1;
					padding-right: 20rpx;
				}
			}

			.useroption {
				display: flex;
				align-items: center;
				padding: 30rpx 10rpx 20rpx;
				margin-top: 20rpx;
				margin-bottom: 30rpx;
				border-top: 1px solid #DEDEDE;
				color: #fff;
				font-size: 24rpx;
				text-align: center;

				.pay,
				.addcart {
					flex: 1;
					padding: 20rpx 0;
				}

				.addcart {
					background-color: #FC5E14;
					border-top-left-radius: 999rpx;
					border-bottom-left-radius: 999rpx;

				}

				.pay {
					border-top-right-radius: 999rpx;
					border-bottom-right-radius: 999rpx;
					background-color: #FF0000;
				}
			}
		}

		.pro-info {
			background-color: #fff;

			image {
				width: 100%;
			}

			.info {
				padding: 0 20rpx;

				.free-discount {
					margin-top: 20rpx;
					display: flex;
					align-items: center;

					.discount,
					.free {
						border: 2rpx solid #FC5E14;
						padding: 0 10rpx;
						font-size: 24rpx;
						color: #FC5E14;
						border-radius: 10rpx;
					}

					.free {
						margin-left: 10rpx;
					}

				}

				.title {
					margin-top: 20rpx;
					font-size: 30rpx;
					color: #000;
					font-weight: 900;
					padding: 10rpx 0;
				}

				.count {
					padding: 10rpx 0;
					color: #BABABA;
					font-size: 26rpx;
					font-weight: 600;
				}

			}

			.price-box {
				padding: 40rpx 20rpx;
				margin-top: 20rpx;
				border-top: 1px solid #DEDEDE;

				.price {
					color: #FC5E14;

					.small {
						font-size: 24rpx;
					}

					.big {
						font-weight: 700;
						font-size: 32rpx;
					}
				}

				.oldprice {
					margin-top: 10rpx;
					color: #BABABA;
					font-size: 24rpx;
					font-weight: 600;
					text-decoration: line-through;
				}
			}

		}

		.pro-desc {
			margin-top: 30rpx;
			background-color: #fff;
			padding: 20rpx 10rpx;
		}

		.paybar {
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100%;
			height: 100rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			background-color: #fff;
			border-top: 1px solid #E7E7E7;
			padding: 0 20rpx;

			.customer {
				margin-right: 40rpx;
				text-align: center;
				font-size: 24rpx;

			}

			.paybtn {
				flex: 1;
				font-size: 24rpx;
				text-align: center;
				background-color: #FF0000;
				color: #fff;
				padding: 20rpx 0;
				border-radius: 999rpx;
			}
		}
	}
</style>
